/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "LOGR_gen_pages.h"
#include "LOGR_independent_links.h"
#include "LOGR_independent_pages.h"


WebIndependentLinkPtr
new_web_independent_link(void)
{
  WebIndependentLinkPtr ol = (WebIndependentLinkPtr)calloc(1, sizeof(WebIndependentLinkStruct));
  if(!ol) {
      fprintf(stderr,"Out of Memory new_web_independent_link\n");
      abort();
  }
  return ol;
}


void
delete_web_independent_link(WebIndependentLinkPtr ol)
{
    if(!ol) return;

    if(ol->sid)
	free(ol->sid); ol->sid = NULL;

    free(ol); ol = NULL;
}

void
LOGR_set_link(WebIndependentLinkPtr ol,TypePage tps, void *nodeptrs, 
	      TypePage tpt, void *nodeptrt)
{
    if(!ol || !nodeptrs || !nodeptrt) return;
    ol->tps = tps;
    ol->nodeptrs = nodeptrs;
    ol->tpt = tpt;
    ol->nodeptrt = nodeptrt;
    ol->sid = LOGR_gen_link_id(tps,nodeptrs, tpt, nodeptrt);
}

void
LOGR_set_link_from_name(WebIndependentLinkPtr ol, char *urls, char *urlt,
			WebStatPtr wsf,GraphFrame *gf)
{
    void *nodeptrs = NULL;
    void *nodeptrt = NULL;
    TypePage tps=0,tpt=0;
    nodeptrs = LOGR_find_info_gen_page(urls,&tps, wsf,gf);
    nodeptrt = LOGR_find_info_gen_page(urlt,&tpt, wsf,gf);
    LOGR_set_link(ol,tps, nodeptrs, tpt, nodeptrt);
}

char *
LOGR_get_link_string_id(WebIndependentLinkPtr ol)
{
    if(!ol) return NULL;
    return ol->sid;
}

TypePage
LOGR_get_link_type_source_page(WebIndependentLinkPtr ol)
{
    if(!ol) return 0;
    return ol->tps;
}

TypePage
LOGR_get_link_type_target_page(WebIndependentLinkPtr ol)
{
    if(!ol) return 0;
    return ol->tpt;
}

void *
LOGR_get_link_source_page(WebIndependentLinkPtr ol)
{
    if(!ol) return NULL;
    return ol->nodeptrs;
}

void *
LOGR_get_link_target_page(WebIndependentLinkPtr ol)
{
    if(!ol) return NULL;
    return ol->nodeptrt;
}

void
LOGR_set_link_naccesses(WebIndependentLinkPtr ol,long num)
{
  if(!ol) return;
  ol->naccesses = num;
}

void
LOGR_inc_link_naccesses(WebIndependentLinkPtr ol)
{
  if(!ol) return;
  ol->naccesses++;
}

long
LOGR_get_link_naccesses(WebIndependentLinkPtr ol)
{
  if(!ol) return 0;
  return ol->naccesses;
}

const char *
LOGR_get_local_node_url(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	return  v ? get_label_vertex(v) : NULL;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? LOGR_get_ip_url(opn) : NULL;
    }
    return NULL;
}

const char *
LOGR_get_local_node_title(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	return  v ? get_attribute(v,"title") : NULL;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	return NULL;
    }
    return NULL;
}

const char *
LOGR_get_local_node_mime(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	return  v ? get_attribute(v,"mime") : NULL;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? LOGR_get_ip_mime(opn) : NULL;
    }
    return NULL;
}

int 
LOGR_get_local_node_size(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	char *size = v ? (char *)get_attribute(v,"size") : NULL; 
	int nsize = size ? atoi(size) : -1 ;
	return nsize;
	/*char *sw = v ? get_weight_vertex(v) : NULL;
	  return  sw ? atoi(sw) : 0;*/
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? (int)LOGR_get_ip_sizebytes(opn) : 0;
    }
    return 0;
}

int 
LOGR_get_local_node_naccesses(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	return  v ? get_counter(v) : 0;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? (int)LOGR_get_ip_naccesses(opn) : 0;
    }
    return 0;
}

int 
LOGR_get_local_node_num(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	return  v ? get_vertex_number(v) : -1;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? (int)LOGR_get_ip_num(opn) : -1;
    }
    return -1;
}

int 
LOGR_get_local_node_http_code(TypePage tp, void *nodeptr)
{
    if(tp == PWEBNODE)
    {
	PNODE v = (PNODE)nodeptr;
	const char *att = v ? get_attribute(v,"code") : NULL;
	return  att ? atoi(att) : -1;
    }
    else if(tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)nodeptr;
	return opn ? LOGR_get_ip_http_code(opn) : -1;
    }
    return -1;
}


const char *
LOGR_get_link_source_url(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_url(ol->tps, ol->nodeptrs);
}


const char *
LOGR_get_link_target_url(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_url(ol->tpt, ol->nodeptrt);
}

const char *
LOGR_get_link_source_title(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_title(ol->tps, ol->nodeptrs);
}

const char *
LOGR_get_link_target_title(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_title(ol->tpt, ol->nodeptrt);
}

const char *
LOGR_get_link_source_mime(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_mime(ol->tps, ol->nodeptrs);
}

const char *
LOGR_get_link_target_mime(WebIndependentLinkPtr ol)
{
    if(!ol)
	return NULL;
    return LOGR_get_local_node_mime(ol->tpt, ol->nodeptrt);
}

int
LOGR_get_link_source_http_code(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_http_code(ol->tps, ol->nodeptrs);
}

int
LOGR_get_link_target_http_code(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_http_code(ol->tpt, ol->nodeptrt);
}

int
LOGR_get_link_source_size(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_size(ol->tps, ol->nodeptrs);
}

int
LOGR_get_link_target_size(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_size(ol->tpt, ol->nodeptrt);
}

int
LOGR_get_link_source_naccesses(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_naccesses(ol->tps, ol->nodeptrs);
}

int
LOGR_get_link_target_naccesses(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_naccesses(ol->tpt, ol->nodeptrt);
}

int
LOGR_get_link_source_num(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_num(ol->tps, ol->nodeptrs);
}

int
LOGR_get_link_target_num(WebIndependentLinkPtr ol)
{
    if(!ol)
	return 0;
    return LOGR_get_local_node_num(ol->tpt, ol->nodeptrt);
}

int
LOGR_is_independent_link(WebIndependentLinkPtr ol,char *urls,char *urlt)
{
    char *sid = NULL;
    int f = 0;

    if(!ol || !urls || !urlt)
	return f;

    sid = LOGR_gen_string_id(urls,urlt);

    if(!strcmp(ol->sid, sid))
	f = 1;

    free(sid); sid = NULL;

    return f;
}

int
LOGR_is_access_il_lower(WebIndependentLinkPtr u, WebIndependentLinkPtr v)
{
  return (u->naccesses < v->naccesses);
}

int
LOGR_is_access_il_bigger(WebIndependentLinkPtr u,WebIndependentLinkPtr v)
{
  return(u->naccesses > v->naccesses);
}



